package top.songxs.yygh.hosp.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.songxs.yygh.cmn.client.DictFeignClient;
import top.songxs.yygh.model.user.Patient;
import top.songxs.yygh.hosp.user.mapper.PatientMapper;
import top.songxs.yygh.hosp.user.service.PatientService;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient>implements PatientService {

    @Autowired
    DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAllUserId(Long userId) {

        //根据userid查询所有就诊人信息列表
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patients = this.baseMapper.selectList(queryWrapper);
        //每个就诊人添加额外的param属性
        patients.forEach(this::packPatient);
        return patients;
    }

    @Override
    public Patient getPatientById(Long id) {

        Patient patient = this.baseMapper.selectById(id);
        this.packPatient(patient);

        return patient;
    }

    private void packPatient(Patient patient) {
        //通过远程调用，得到编码对应具体内容，查询数据字典表内容
        //就诊人证件类型
        String certificatesTypeString = dictFeignClient.getName(patient.getCertificatesType());

        //省市区的名字
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());//省
        String cityString = dictFeignClient.getName(patient.getCityCode());//市
        String districtString = dictFeignClient.getName(patient.getDistrictCode());//区

        patient.getParam().put("certificatesTypeString",certificatesTypeString);
        patient.getParam().put("provinceString",provinceString);
        patient.getParam().put("cityString",cityString);
        patient.getParam().put("districtString",districtString);
        patient.getParam().put("fullAddress",provinceString+cityString+districtString+patient.getAddress());
    }
}
